package com.wl.service.impl;


import com.wl.domain.SysPermission;
import com.wl.domain.SysUser;
import com.wl.mapper.UserMapper;
import com.wl.service.UserService;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //根据用户名查询用户信息
        SysUser sysUser = userMapper.loadUserByUsername(username);
        //根据用户名查询权限信息
        List<SysPermission> listperssion = userMapper.findPermissionByUsername(username);
//        List<GrantedAuthority> authorities = new ArrayList<>();
//        for (SysPermission syspermission: listperssion) {
//            GrantedAuthority grantedAuthority = new SimpleGrantedAuthority(syspermission.getAuthority());
//            authorities.add(grantedAuthority);
//        }
//        sysUser.setAuthorities(authorities);
        //sysUser
        sysUser.setPermissions(listperssion);
        return sysUser;
    }

    @Override
    public SysUser findOne(Map<String, Object> pasMap) {
        return userMapper.findOne(pasMap);
    }

    @Override
    public SysUser findOneAuth(Map<String, Object> pasMap) {
        return userMapper.findOneAuth(pasMap);
    }

    @Override
    public List<SysPermission> findPermissionByUsername(String username) {
        return userMapper.findPermissionByUsername(username);
    }

    @Override
    public String testServiceExcep(int i) {

        if (i > 4){
            int s = i/0;
        }

        return "返回正常";
    }
}
